package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {


    @Autowired
    private DestinationRepository destinationRepository;

    @Autowired
    private RegionRepository regionRepository;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void insert(Destination destination) {
        destinationRepository.insert(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.insert(destination);
    }

    @Override
    public Destination get(String id) {
        //orElse方法从optional中取值，如果取不到返回null
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        List<String> list = regionRepository.findById(rid).orElse(null).getRefIds();
        List<Destination> destinations = destinationRepository.findByIdIn(list);
        return destinations;
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getKeyword())) {
            Criteria c = Criteria.where("name").regex(qo.getKeyword());
            query.addCriteria(c);
        }
        //加入parentId作为查询条件
        if(StringUtils.hasLength(qo.getParentId())){
            Criteria c = Criteria.where("parentId").is(qo.getParentId());
            query.addCriteria(c);
        }
        else{
            Criteria c = Criteria.where("parentId").is(null);
            query.addCriteria(c);
        }
        Page<Destination> page = DBHelper.query(mongoTemplate, Destination.class, qo, query);
        return page;
    }

    @Override
    public List<Destination> queryToasts(DestinationQuery qo) {
        String currid = qo.getParentId();
        //利用当前的currid找到他的名字-》他的父亲的名字-》他的父亲的父亲的名字……
        //使用循环的方式查询
//        List<Destination> list = new ArrayList<>();
//        while(StringUtils.hasLength(currid)){
//            Destination destination = destinationRepository.findById(currid).orElse(null);
//            list.add(destination);
//            currid = destination.getParentId();
//        }
//        Collections.reverse(list);

        //使用linkedlist实现
//        LinkedList<Destination> list = new LinkedList<>();
//        while(StringUtils.hasLength(currid)){
//            Destination destination = destinationRepository.findById(currid).orElse(null);
//            list.addFirst(destination);
//            currid = destination.getParentId();
//        }

        //递归方式实现
        //递归：在方法内部再次调用自己；递归需要有一个停止条件
        LinkedList<Destination> list = new LinkedList<>();
        insertDestination(currid, list);
        return list;
    }







    @Override
    public List<Destination> queryToasts(String id) {
        LinkedList<Destination> list = new LinkedList<>();
        insertDestination(id, list);
        return list;
    }




    @Override
    public List<Destination> search(String regionId) {
        List<Destination> list = new ArrayList<>();
        if("-1".equals(regionId)){
           list = destinationRepository.findByParentName("中国");
        }
        else{
           list = this.getDestByRegionId(regionId);
        }
        for (Destination destination : list) {
            //获取遍历到的每一个目的地的id
            String id = destination.getId();
            //方案1.
            //通过id查询子目的地列表，子目的地的特征是parentId为id
//            List<Destination> childlist = destinationRepository.findByParentId(id);
//            if(childlist.size()>5)
//            {
//                destination.setChildren(childlist.subList(0,5));
//            }
//            else {
//                destination.setChildren(childlist);
//            }

            Pageable pageable = PageRequest.of(0,5);
            List<Destination> childlist = destinationRepository.findByParentId(id, pageable);
            destination.setChildren(childlist);
        }

        return list;
    }

    //根据当前id，将id对应的目的地对象插入到列表中
    public void insertDestination(String currid, LinkedList<Destination> ll){
        if(StringUtils.hasLength(currid)) {
            Destination destination = destinationRepository.findById(currid).orElse(null);
            ll.addFirst(destination);
            insertDestination(destination.getParentId(), ll);
        }
    }




}
